package org.example.apitest.transform;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.streaming.api.datastream.ConnectedStreams;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.streaming.api.functions.co.CoMapFunction;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;
import org.example.apitest.beans.SensorReading;

public class TransformsTest4MultipleStreams {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        // 从文件中读取数据
        DataStream<String> inputStream = env.readTextFile("src/main/resources/sensor.txt");

        // 转换为SensorReading类型
        DataStream<SensorReading> dataStream = inputStream.map((MapFunction<String, SensorReading>) SensorReading::new);

        // 1. 分流,按照温度值30度为界,分为两条流
        OutputTag<SensorReading> highTag = new OutputTag<>("highTemp", TypeInformation.of(SensorReading.class));
        OutputTag<SensorReading> lowTag = new OutputTag<>("logTemp", TypeInformation.of(SensorReading.class));
        SingleOutputStreamOperator<SensorReading> outputStream = dataStream.process(new ProcessFunction<SensorReading, SensorReading>() {
            @Override
            public void processElement(SensorReading value, Context ctx, Collector<SensorReading> out) throws Exception {
                if (value.getTemperature() > 30) {
                    ctx.output(highTag, value);
                } else {
                    ctx.output(lowTag, value);
                }
            }
        });
        DataStream<SensorReading> highTempStream = outputStream.getSideOutput(highTag);
        DataStream<SensorReading> lowTempStream = outputStream.getSideOutput(lowTag);

        highTempStream.print();
        lowTempStream.print();

        // 2. 合流 connect,将高温流转换成二元组类型,与低温流连接合并之后,输出状态信息
        DataStream<Tuple2<String, Double>> warningStream = highTempStream.map(value -> new Tuple2<>(value.getId(), value.getTemperature())).returns(Types.TUPLE(Types.STRING, Types.DOUBLE));
        ConnectedStreams<Tuple2<String, Double>, SensorReading> connectedStreams = warningStream.connect(lowTempStream);
        SingleOutputStreamOperator<Object> connectStream = connectedStreams.map(new CoMapFunction<Tuple2<String, Double>, SensorReading, Object>() {
            @Override
            public Object map1(Tuple2<String, Double> value) throws Exception {
                return new Tuple3<String, Double, String>(value.f0, value.f1, "high temp warning");
            }

            @Override
            public Object map2(SensorReading value) throws Exception {
                return new Tuple2<String, String>(value.getId(), "normal");
            }
        });
        connectStream.print();

        // 3. union 联合多条流
        DataStream<SensorReading> unionStream = highTempStream.union(lowTempStream);
        unionStream.print();

        env.execute();
    }
}
